package com.hoperun.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hoperun.bean.AppUserFriends;
import com.hoperun.controller.common.BaseController;
import com.hoperun.framework.ex.XHZException;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.models.DataView;
import com.hoperun.framework.query.Criteria;
import com.hoperun.service.IAppUserFriendsSV;
import com.hoperun.utils.Constant;
import com.hoperun.utils.ErrorCode;

/**
 * Copyright: Copyright (c) 2015 XHZ <br/>
 * ClassName: AppUserFriendsController.java
 * 
 * @Description:
 * 
 * @version: v1.0.0
 * @author: fuyu
 * @date: 2014-7-23 上午9:11:06
 * 
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2014-7-23
 *        fuyu v1.0.0 修改理由
 */
@Controller
@RequestMapping("/admin/app/user/friends")
@RequiresUser
public class AppUserFriendsController extends BaseController {

	@Resource
	private IAppUserFriendsSV appUserFriendsSV;

	@ModelAttribute
	public void populateModel(final HttpServletRequest request,
			ModelMap modelMap) {
		try {
			super.initMenus(request, modelMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/{menu}/{menuNext}")
	public String index(@PathVariable long menu, @PathVariable long menuNext,
			ModelMap modelMap) {

		modelMap.put("menuType", menu);
		modelMap.put("menuNextType", menuNext);

		return "admin:appuserfriends.list";
	}

	@RequestMapping("/page")
	@ResponseBody
	public BootGridPage<AppUserFriends> identityUserPage(
			BootGrid<AppUserFriends> grid, AppUserFriends appUserFriends,
			ModelMap modelMap) {
		BootGridPage<AppUserFriends> pageModel = new BootGridPage<AppUserFriends>();

		try {
			Criteria criteria = new Criteria();

			if (null == grid)
				return new BootGridPage<AppUserFriends>();

			pageModel = appUserFriendsSV.queryPage(criteria, grid);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return pageModel;
	}

	@RequestMapping("/create")
	public String create(ModelMap modelMap) {
		modelMap.addAttribute("method", "save");
		return "adminAlone:appuserfriends.info";
	}

	@RequestMapping("/modify/{id}")
	public String modify(@PathVariable long id, ModelMap modelMap,
			final HttpServletRequest request) {
		AppUserFriends appUserFriends = null;
		try {
			appUserFriends = appUserFriendsSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("method", "update");
		modelMap.addAttribute("row", appUserFriends);
		return "adminAlone:appuserfriends.info";
	}

	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable long id, ModelMap modelMap,
			final HttpServletRequest request) {
		AppUserFriends appUserFriends = null;
		try {
			appUserFriends = appUserFriendsSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("detail", appUserFriends);
		return "adminAlone:appuserfriends.detail";
	}

	@RequestMapping("/save")
	@ResponseBody
	public DataView save(AppUserFriends appUserFriends) {
		DataView dataView = new DataView();

		try {
			appUserFriendsSV.save(appUserFriends);

			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("新增成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataView;
	}

	@RequestMapping("/update")
	@ResponseBody
	public DataView update(AppUserFriends appUserFriends) {
		DataView dataView = new DataView();

		if (null == appUserFriends
				|| StringUtils.isBlank(appUserFriends.getFriendAccount())
				|| StringUtils.isBlank(appUserFriends.getUserAccount())) {
			throw new XHZException(ErrorCode.MISS_PARAM, null,
					new Object[] { "id信息" });
		}

		try {
			appUserFriendsSV.update(appUserFriends);

			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataView;
	}

	@RequestMapping("/find/{id}")
	@ResponseBody
	public AppUserFriends findOne(@PathVariable long id) {
		AppUserFriends appUserFriends = null;
		try {
			appUserFriends = appUserFriendsSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return appUserFriends;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public DataView delete(Long[] ids) {
		DataView dataView = new DataView();
		AppUserFriends appUserFriends = null;
		try {
			if (null == ids) {
				throw new XHZException(ErrorCode.ILLEGAL_PARAM, null,
						new Object[] { "id" });
			}

			for (Long id : ids) {
				appUserFriends = new AppUserFriends();
				// appUserFriendsSV.update(appUserFriends);

			}
			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return dataView;
	}
}
